//
//  MyResumeListViewController.m
//  InviteJob
//
//  Created by LangLang on 17/10/30.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "MyResumeListViewController.h"
#import "ResumeViewController.h"

@interface MyResumeListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIButton *navRightButton;
@property (nonatomic,strong) NSMutableArray *dataList;

@end

@implementation MyResumeListViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self postData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = strColor(@"f6f6f6");
    TitleView(@"我的简历")

    // right item
    self.navRightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    [self.navRightButton setImage:[UIImage imageNamed:@"icon_nav_public_add"] forState:UIControlStateNormal];
    self.navRightButton.imageEdgeInsets = UIEdgeInsetsMake(0,15, 0, -15);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:self.navRightButton];
    self.navigationItem.rightBarButtonItem = rightItem;
    // 事件绑定
    [self.navRightButton addTarget:self action:@selector(navAddAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self postData];
    [self layoutUI];

    
}
-(NSMutableArray *)dataList{
    if (!_dataList) {
        _dataList = @[].mutableCopy;
    }
    return _dataList;
}

-(void)navAddAction{
    ResumeViewController *resume = [ResumeViewController new];
    [self.navigationController pushViewController:resume animated:YES];
}

-(void)layoutUI{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(12, 0, WIDTH-24, HEIGHT-64) style:UITableViewStyleGrouped];
    _tableView.dataSource   =self;
    _tableView.delegate     =self;
    _tableView.backgroundColor = strColor(@"f6f6f6");
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.tableFooterView = [UIView new];
    _tableView.separatorStyle = UITableViewCellEditingStyleNone;
    _tableView.rowHeight = 60;
    [self.view addSubview:_tableView];
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataList.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10.0f;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cells"];
    if (!cell) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cells"];
    }else{
        while ([cell.contentView.subviews lastObject]) {
            [[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
   
    BaseModel *model = _dataList[indexPath.row];
    
    UILabel *labelCateName = [[UILabel alloc] init];
    labelCateName.text = model.cate_name;
    labelCateName.textColor = strColor(@"333");
    labelCateName.textAlignment = NSTextAlignmentLeft;
    labelCateName.font = [UIFont systemFontOfSize:16];
    [cell.contentView addSubview:labelCateName];
    [labelCateName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(cell.contentView).offset(-5);
        make.left.equalTo(@12);
        make.width.equalTo(@150);
    }];
    
    
    UILabel *labelTime = [[UILabel alloc] init];
    labelTime.text = model.create_time;
    labelTime.textColor = strColor(@"999");
    labelTime.textAlignment = NSTextAlignmentRight;
    labelTime.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:labelTime];
    [labelTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(cell.contentView).offset(-5);
        make.right.equalTo(@-12);
        make.width.equalTo(@150);
    }];
    
    UIView *viewLine = [[UIView alloc] initWithFrame:CGRectMake(0, 50, WIDTH-24, 10)];
    viewLine.backgroundColor = strColor(@"f6f6f6");
    [cell.contentView addSubview:viewLine];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    cell.separatorInset=UIEdgeInsetsZero;
    cell.layoutMargins=UIEdgeInsetsZero;
    cell.preservesSuperviewLayoutMargins=NO;
}

#pragma mark -- 删除 --
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return   UITableViewCellEditingStyleDelete;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"确定要删除该简历吗？"  preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *determineAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        BaseModel *model = _dataList[indexPath.row];
        [self postDeletDataResumeId:model.resume_id];
        [self.dataList removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
    }];
    
    [actionSheetController addAction:determineAction];
    [actionSheetController addAction:cancelAction];
    
    [self presentViewController:actionSheetController animated:YES completion:nil];
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}

#pragma mark -- 点击 --

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BaseModel *model = _dataList[indexPath.row];
    
    if (!_workIdStr) {
        //编辑简历
        ResumeViewController *resume = [ResumeViewController new];
        resume.strResumeId = model.resume_id;
        [self.navigationController pushViewController:resume animated:YES];
        return;
    }
    
    //投递简历
    UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"投递此简历？"  preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *determineAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        isUser
        NSDictionary * datadic = @{@"zp_id":self.workIdStr,
                                   @"user_id":user.user_id,
                                   @"token":user.token,
                                   @"resume_id":model.resume_id};
        SHuiMBhubNoTimeStr(@"");
        [PostNetWorking postNetWorkingStr:RequestUrlStr(main_sendResume_api) andparameters:datadic Success:^(id responseObject) {
            SHuiMBHidleYes;
            if (codeSucess) {
                
                SHuiMBhudTextTimeStr(@"投递成功！");
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.navigationController popViewControllerAnimated:YES];
                });
                
            }else {
                
                SHuiMBhudTextTimeStr(responseObject[@"mark"]);
            }
            
        } failed:^(NSError *error) {
            
            SHuiMBHidleYes;
            SHuiMBhudTextNetError;
        }];
        
    }];
    
    [actionSheetController addAction:determineAction];
    [actionSheetController addAction:cancelAction];
    
    [self presentViewController:actionSheetController animated:YES completion:nil];
}

#pragma mark -- 数据源 --
-(void)postData{
    isUser 
    NSString *strUrl = [NSString stringWithFormat:post_getMyResumeList_api];
    
    NSDictionary *dicParameter = @{@"user_id":user.user_id,
                                   @"token":user.token};
    
    [PostNetWorking postNetWorkingStr:strUrl andparameters:dicParameter Success:^(id responseObject) {
        
        if ([responseObject[@"code"] intValue] == 200) {
            self.dataList = [BaseModel parsingDateFromResultDict:responseObject andData:@""];
            
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                if (self.dataList.count>0) {
                    [self hideNoDataView];
                }else {
                    [self shopNoDataViewBackgroundColor:[UIColor clearColor] withImage:@"img_home_no_news" withLabelMsg:@"没有添加简历" withButton:@"" isY:0];
                    
                }
                
                [self.tableView reloadData];
                
            });
        }else{
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
        
    } failed:^(NSError *error) {
        
        SHuiMBhudTextTimeStr(@"网络错误");
    }];
}


#pragma mark -- 删除 --
-(void)postDeletDataResumeId:(NSString *)resumeId{
    isUser
    NSString *strUrl = [NSString stringWithFormat:post_delResume_api];
    
    NSDictionary *dicParameter = @{@"user_id":user.user_id,
                                   @"token":user.token,
                                   @"resume_id":resumeId};
    
    [PostNetWorking postNetWorkingStr:strUrl andparameters:dicParameter Success:^(id responseObject) {
        
        if ([responseObject[@"code"] intValue] == 200) {
            
            SHuiMBhudTextTimeStr(@"删除成功");
            dispatch_async(dispatch_get_main_queue(), ^{
                [self postData];
                
            });
        }else{
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
        
    } failed:^(NSError *error) {
        
        SHuiMBhudTextTimeStr(@"网络错误");
    }];
}

@end
